package com.web.servlet;

import com.web.dao.GradeDao;
import com.web.dao.Testdao;
import com.web.daoimpl.ClassDaoImpl;
import com.web.daoimpl.CourseDaoImpl;
import com.web.daoimpl.GradeDaoImpl;
import com.web.entity.ClassInfo;
import com.web.entity.Course;
import com.web.entity.Grade;
import com.web.entity.Student;
import com.web.entity.Test;
import com.web.service.Testdaoimpl;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 17612
 */
@WebServlet("/gasgs")
public class GetAndSubmitGradeServlet extends HttpServlet{
    Testdao td = new Testdaoimpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ///Dorothy
        String[] s = new String[15];
        for(int i = 0; i < 15; i++){
            s[i] = req.getParameter("select"+i);
            System.out.println(s[i]);
        }
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        System.out.println(session.getAttribute("courseid"));
        System.out.println(session.getAttribute("classid"));
        int courseid = (int) session.getAttribute("courseid");
        //Dorothy:计算分数
        List<Test> test = td.getOneByCourseid(courseid);
        int score = 0;
        for(int i = 0; i < 15; i++){
            if(s[i].equals(test.get(i).getAnswer())){
                score += test.get(i).getScore();
                System.out.println(score);
            }
        }
        //
        String classid = (String) session.getAttribute("classid");
        
          
        //1.获取学号
        Student stu=(Student)req.getSession().getAttribute("s");
        System.out.println(stu);
        String sid =stu.getSid();
        
        //2.获取班级名称
        ClassDaoImpl cd1 = new ClassDaoImpl();
        ClassInfo cif = cd1.getClassByClassID(classid);
        String classname = cif.getClassName();
        
        
        //3.获取课程名称
        CourseDaoImpl cd2 = new CourseDaoImpl();
        Course cou = cd2.getOneCourseNameByCourseID(courseid);
        String coursename = cou.getCourseName();
        
        //4.获取成绩,在上面代码里的score
        
        //封装Grade
        Grade g = null;
        g = new Grade(sid,classname,coursename,score);
        
        //插入
        GradeDao grade=new GradeDaoImpl(); 
        grade.InsertStudentGrade(g);
        
        req.setAttribute("grade", score);
        req.getRequestDispatcher("StudentGrade.jsp").forward(req, resp);
    }
    

    
}
